import { flyUserDetail } from '@/api/flyUser';
import { generateUUID } from '@/utils/tools';

export default {
  data() {
    return {
    };
  },
  methods: {
    // 获取飞来用户信息
    getUserInfo() {
      this.flyLoading = true;
      flyUserDetail(this.flyUserId).then(res => {
        this.flyLoading = false;
        if (res.code !== 200) {
          this.$$message.error(res.message);
          return;
        }
        this.initSupplier(res.data.userInfo);
        this.initPayAccount(res.data.accountsReceivable);
        this.initAccounts(res.data.accountsInfoList);
      }).catch(err => {
        this.flyLoading = false;
        console.log(err);
      });
    },
    // 飞来用户回填供应商
    initSupplier(info) {
      Object.keys(info).forEach(key => {
        if (key === 'region') {
          info[key] && (this.supplierInfo[key] = info[key].replace(/-/g, ','));
        } else if (this.supplierInfo.hasOwnProperty(key) && (info[key] || info[key] === 0)) {
          this.supplierInfo[key] = info[key];
        }
        if (key === 'sex') {
          this.supplierInfo[key] = this.sexMap(info[key]);
        }
        info.userName && (this.supplierInfo.channelName = info.userName);
      });
    },
    // 小程序性别的值不一致
    sexMap(sex) {
      return {
        0: 0,
        1: 0,
        2: 1
      }[sex];
    },
    // 飞来用户回填收款账户
    initPayAccount(list) {
      if (!list || list.length === 0) {
        return;
      }
      list.forEach(el => {
        if (el.id) {
          delete el.id;
        }
        el.province = el.province.replace(/-/g, ',');
      });
      this.accountsReceivable.list = list;
    },
    // 飞来用户回填账号信息
    initAccounts(list) {
      if (!list || list.length === 0) {
        return;
      }
      list.forEach((el, index) => {
        const item = {
          accountsInfo: el,
          _uuid: generateUUID(),
          priceInfo: {
            adjustPriceReason: ''
          },
          contractInfo: {
            agentId: this.userInfo.id,
            agentName: this.userInfo.userName,
            workWxCode: '',
            signDate: [],
            cooperationMode: '',
            cooperationType: '',
            signPoint: undefined,
            legalCode: null,
            settledScreenshot: []
          } // 签约信息
        };
        this.$set(this.accountsInfoList, index, item);
      });
    }
  }
};
